package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("permission")
public class PermissionController {

    @DubboReference
    private PermissionService permissionService;


    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    @GetMapping("all")
    public Result findAll(){
        List<Permission> permissionList = permissionService.findAll();
        if (permissionList==null){
            return new Result(false, MessageConst.QUERY_PERMISSION_FAIL);
        }
        return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,permissionList);
    }

    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean){
        PageResult pageResult = permissionService.findPage(queryPageBean);
        if (pageResult==null){
            return new Result(false,MessageConst.QUERY_PERMISSION_FAIL);
        }
        return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,pageResult);
    }


    @PreAuthorize("hasAuthority('PERMISSION_ADD')")
    @PostMapping("add")
    public Result add(@RequestBody Permission permission){
        boolean flag = permissionService.add(permission);
        if (!flag){
            return new Result(false,MessageConst.ADD_PERMISSION_FAIL);
        }
        return new Result(true,MessageConst.ADD_PERMISSION_SUCCESS);
    }

    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    @GetMapping("findById")
    public Result findById(Integer id){
        Permission permission = permissionService.findById(id);
        if (permission == null){
            return new Result(false,MessageConst.QUERY_PERMISSION_FAIL);
        }
        return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,permission);
    }


    @PreAuthorize("hasAuthority('PERMISSION_EDIT')")
    @PostMapping("update")
    public Result update(@RequestBody Permission permission){
        boolean flag = permissionService.update(permission);
        if (!flag){
            return new Result(false,MessageConst.EDIT_USER_FAIL);
        }
        return new Result(true,MessageConst.EDIT_USER_SUCCESS);
    }


    @PreAuthorize("hasAuthority('PERMISSION_DELETE')")
    @GetMapping("delete")
    public Result delete(Integer id){
        boolean flag = permissionService.deleteById(id);
        if (!flag){
            return new Result(false,MessageConst.QUERY_PERMISSION_FAIL);
        }
        return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS);
    }

}
